<?php 
class Scoring_model extends CI_Model
{
	public function __construct(){}
	
	public function getAllApplicant($award_id)
	{
		$this->db->where('user_application_award_id',$award_id);
		$this->db->where('user_application_application_status','Complete');
		$result = $this->db->get('scopus_user_application');
		$resultData=array();
		if($result->num_rows()>0){
			$resultData = $result->result_array();
		}
		return $resultData;
	}	
	
	public function getScoring($app_id,$award_id)
	{	
		$this->db->where('admin_review_applicant_id',$app_id);
		$this->db->where('admin_review_admin_id',$this->session->userdata('admin_id'));
		$this->db->where('admin_review_award_id',$award_id);
		$result = $this->db->get('scopus_admin_review');
		/*$from = 'admin_review';	
		$result = $this->common_model->selectData($from, '*', '', '');*/
		$resultData=array();
		if($result->num_rows()>0){
			$resultData = $result->result_array();
		}
		return $resultData;
	}	
	
	public function getAward()
	{
	   $from = 'award';
		$result = $this->common_model->selectData($from, '*', '', '');
		$resultData=array();
		if($result->num_rows()>0){
			$resultData = $result->result_array();
		}
		return $resultData;
	}	
		
	
	
	public function addScoring($value='')
	{
		if (empty($value))
		{
			return FALSE;
		}
		$date=date('Y-m-d H:i:s');
		$id = $this->session->userdata('admin_id');	
		$this->db->where('admin_review_admin_id',htmlspecialchars(strip_tags($id),ENT_QUOTES));
		$this->db->where('admin_review_event_id',htmlspecialchars(strip_tags($value['award_event_id']),ENT_QUOTES));
		$this->db->where('admin_review_award_id',htmlspecialchars(strip_tags($value['user_application_award_id']),ENT_QUOTES));
		$this->db->where('admin_review_applicant_id',htmlspecialchars(strip_tags($value['user_application_applicant_id']),ENT_QUOTES));
		$this->db->where('admin_review_status','Active');
		$q = $this->db->get('scopus_admin_review');
		if($q->num_rows() == 1){
			$data=array(
				'admin_review_scopus_citation_including_self' => htmlspecialchars(strip_tags($value['citation_including_self']),ENT_QUOTES),
				'admin_review_scopus_citation_excluding_self' => htmlspecialchars(strip_tags($value['citation_excluding_self']),ENT_QUOTES),
				'admin_review_scopus_hindex_include_self' => htmlspecialchars(strip_tags($value['hindex_include_self']),ENT_QUOTES),
				'admin_review_scopus_hindex_exclude_self' => htmlspecialchars(strip_tags($value['hindex_exclude_self']),ENT_QUOTES),
				'admin_review_journals_submitted' => htmlspecialchars(strip_tags($value['journals_sumbitted']),ENT_QUOTES),
				'admin_review_journals_scopus' => htmlspecialchars(strip_tags($value['journals_scopus']),ENT_QUOTES),
				'admin_review_journals_avcitationperpaper' => htmlspecialchars(strip_tags($value['journals_avcitationperpaper']),ENT_QUOTES),
				'admin_review_patents_submitted' => htmlspecialchars(strip_tags($value['patents_submitted']),ENT_QUOTES),
				'admin_review_patents_scopus' => htmlspecialchars(strip_tags($value['patents_scopus']),ENT_QUOTES),
				'admin_review_cp_submitted' => htmlspecialchars(strip_tags($value['cp_submitted']),ENT_QUOTES),
				'admin_review_cp_scopus' => htmlspecialchars(strip_tags($value['cp_scopus']),ENT_QUOTES),
				'admin_review_books_submitted' => htmlspecialchars(strip_tags($value['books_submitted']),ENT_QUOTES),
				'admin_review_book_scopus' => htmlspecialchars(strip_tags($value['books_scopus']),ENT_QUOTES),
				'admin_review_modified_by' => $id,
				'admin_review_modified_date' => htmlspecialchars(strip_tags($date),ENT_QUOTES),
				'admin_review_status' => 'Active'
				);
			$q_r = $q->result_array();
			$this->db->update('scopus_admin_review',$data);
			return true;
		}
		else {
			$data=array(
		        'admin_review_admin_id' => htmlspecialchars(strip_tags($id),ENT_QUOTES),
				'admin_review_event_id' =>htmlspecialchars(strip_tags($value['award_event_id']),ENT_QUOTES),
				'admin_review_award_id' => htmlspecialchars(strip_tags($value['user_application_award_id']),ENT_QUOTES),
				'admin_review_applicant_id' => htmlspecialchars(strip_tags($value['user_application_applicant_id']),ENT_QUOTES),
				'admin_review_scopus_citation_including_self' => htmlspecialchars(strip_tags($value['citation_including_self']),ENT_QUOTES),
				'admin_review_scopus_citation_excluding_self' => htmlspecialchars(strip_tags($value['citation_excluding_self']),ENT_QUOTES),
				'admin_review_scopus_hindex_include_self' => htmlspecialchars(strip_tags($value['hindex_include_self']),ENT_QUOTES),
				'admin_review_scopus_hindex_exclude_self' => htmlspecialchars(strip_tags($value['hindex_exclude_self']),ENT_QUOTES),
				'admin_review_journals_submitted' => htmlspecialchars(strip_tags($value['journals_sumbitted']),ENT_QUOTES),
				'admin_review_journals_scopus' => htmlspecialchars(strip_tags($value['journals_scopus']),ENT_QUOTES),
				'admin_review_journals_avcitationperpaper' => htmlspecialchars(strip_tags($value['journals_avcitationperpaper']),ENT_QUOTES),
				'admin_review_patents_submitted' => htmlspecialchars(strip_tags($value['patents_submitted']),ENT_QUOTES),
				'admin_review_patents_scopus' => htmlspecialchars(strip_tags($value['patents_scopus']),ENT_QUOTES),
				'admin_review_cp_submitted' => htmlspecialchars(strip_tags($value['cp_submitted']),ENT_QUOTES),
				'admin_review_cp_scopus' => htmlspecialchars(strip_tags($value['cp_scopus']),ENT_QUOTES),
				'admin_review_books_submitted' => htmlspecialchars(strip_tags($value['books_submitted']),ENT_QUOTES),
				'admin_review_book_scopus' => htmlspecialchars(strip_tags($value['books_scopus']),ENT_QUOTES),
				'admin_review_created_date' => htmlspecialchars(strip_tags($date),ENT_QUOTES),
				'admin_review_status' => 'Active'
				);
			$this->db->insert('scopus_admin_review',$data);
			return true;
		}
		//return $key;
	}
	
	public function updateScore($value="")
	{
		if (empty($value))
		{
			return FALSE;
		}
		$date=date('Y-m-d H:i:s');
		$id = $this->session->userdata('admin_id');	
		$id1 = $this->session->userdata('user_id');
		$id2 = $this->session->userdata('award_id');
		$data=array(
				 'admin_review_admin_id' => htmlspecialchars(strip_tags($id),ENT_QUOTES),
				'admin_review_event_id' =>'1',
				'admin_review_award_id' => htmlspecialchars(strip_tags($id2),ENT_QUOTES),
				'admin_review_applicant_id' => htmlspecialchars(strip_tags($id1),ENT_QUOTES),
				'admin_review_scopus_citation_including_self' => htmlspecialchars(strip_tags($value['citation_including_self']),ENT_QUOTES),
				'admin_review_scopus_citation_excluding_self' => htmlspecialchars(strip_tags($value['citation_excluding_self']),ENT_QUOTES),
				'admin_review_scopus_hindex_include_self' => htmlspecialchars(strip_tags($value['hindex_include_self']),ENT_QUOTES),
				'admin_review_scopus_hindex_exclude_self' => htmlspecialchars(strip_tags($value['hindex_exclude_self']),ENT_QUOTES),
				'admin_review_journals_submitted' => htmlspecialchars(strip_tags($value['journals_sumbitted']),ENT_QUOTES),
				'admin_review_journals_scopus' => htmlspecialchars(strip_tags($value['journals_scopus']),ENT_QUOTES),
				'admin_review_journals_avcitationperpaper' => htmlspecialchars(strip_tags($value['journals_avcitationperpaper']),ENT_QUOTES),
				'admin_review_patents_submitted' => htmlspecialchars(strip_tags($value['patents_submitted']),ENT_QUOTES),
				'admin_review_patents_scopus' => htmlspecialchars(strip_tags($value['patents_scopus']),ENT_QUOTES),
				'admin_review_cp_submitted' => htmlspecialchars(strip_tags($value['cp_submitted']),ENT_QUOTES),
				'admin_review_cp_scopus' => htmlspecialchars(strip_tags($value['cp_scopus']),ENT_QUOTES),
				'admin_review_books_submitted' => htmlspecialchars(strip_tags($value['books_submitted']),ENT_QUOTES),
				'admin_review_book_scopus' => htmlspecialchars(strip_tags($value['books_scopus']),ENT_QUOTES),
				'admin_review_created_date' => htmlspecialchars(strip_tags($date),ENT_QUOTES),
				'admin_review_status' => 'Active'
				);
		$where = array('admin_review_admin_id' => $id);
		$this->db->where($where);
		$this->db->update($this->db->dbprefix('admin_review'), $data);
	}
	public function addSecondScore($score){
		$data=array($score['name']=> $score['xy']);
		$this->db->update($this->db->dbprefix('admin_review'), $data);
	}
	

	public function panelist_scoring($data){
		$insert = array();
		if($data['type'] == 'scopus') {
			$insert['panelist_review_scopus_score'] = htmlspecialchars(strip_tags($data['score'],ENT_QUOTES));
			$insert['panelist_review_scopus_weighted_score'] = htmlspecialchars(strip_tags($data['ws_score'],ENT_QUOTES));
		}
		if($data['type'] == 'journals_score') {
			$insert['panelist_review_journals_score'] = htmlspecialchars(strip_tags($data['score'],ENT_QUOTES));
			$insert['panelist_review_journals_weighted_score'] = htmlspecialchars(strip_tags($data['ws_score'],ENT_QUOTES));
		}
		if($data['type'] == 'patent_score') {
			$insert['panelist_review_patent_score'] = htmlspecialchars(strip_tags($data['score'],ENT_QUOTES));
			$insert['panelist_review_patent_weighted_score']=htmlspecialchars(strip_tags($data['ws_score'],ENT_QUOTES));
		}
		if($data['type'] == 'cp_score') {
			$insert['panelist_review_cp_score']=htmlspecialchars(strip_tags($data['score'],ENT_QUOTES));
			$insert['panelist_review_cp_weighted_score']=htmlspecialchars(strip_tags($data['ws_score'],ENT_QUOTES));
		}
		if($data['type'] == 'books_score') {
			$insert['panelist_review_books_score']=htmlspecialchars(strip_tags($data['score'],ENT_QUOTES));
			$insert['panelist_review_books_weighted_score']=htmlspecialchars(strip_tags($data['ws_score'],ENT_QUOTES));
		}
		if($data['type'] == 'social_score') {
			$insert['panelist_review_social_score']=htmlspecialchars(strip_tags($data['score'],ENT_QUOTES));
			$insert['panelist_review_social_weighted_score']=htmlspecialchars(strip_tags($data['ws_score'],ENT_QUOTES));
		}
		if($data['type'] == 'research_impact') {
			$insert['panelist_review_research_impact']=htmlspecialchars(strip_tags($data['score'],ENT_QUOTES));
		}
		if($data['type'] == 'comment') {
			$insert['panelist_review_comment']=htmlspecialchars(strip_tags($data['score'],ENT_QUOTES));
		}
		$panelist_review_event_id = $data['event_id'];
		$panelist_review_award_id = $data['award_id'];
		$panelist_review_panelist_id = $this->session->userdata('admin_id');
		$panelist_review_applicant_id = $data['applicant_id'];
		
			$this->db->where('panelist_review_event_id',$panelist_review_event_id);
			$this->db->where('panelist_review_award_id',$panelist_review_award_id);
			$this->db->where('panelist_review_panelist_id',$panelist_review_panelist_id);
			$this->db->where('panelist_review_applicant_id',$panelist_review_applicant_id);
			$query=$this->db->get('scopus_panelist_review');
			if($query->num_rows() == 1){
				$result = $query->result();
				$panelist_review_id = $result[0]->panelist_review_id;
				
				$insert['panelist_review_publication_weighted_score'] = $result[0]->panelist_review_journals_weighted_score + $result[0]->panelist_review_patent_weighted_score + $result[0]->panelist_review_cp_weighted_score + $result[0]->panelist_review_books_weighted_score;
				$insert['panelist_review_total_weighted_score']=  $result[0]->panelist_review_scopus_weighted_score + $result[0]->panelist_review_publication_weighted_score + $result[0]->panelist_review_social_weighted_score;
				$insert['panelist_review_modified_date'] = date('Y-m-d H:i:s');
				$insert['panelist_review_modified_by'] = 4;
				$this->db->where('panelist_review_id',$panelist_review_id);
				$query1 = $this->db->update('scopus_panelist_review',$insert);
				if($query1){
					return true;
				}
				else {
					return false;
				}
			}else {
				$insert['panelist_review_event_id'] = htmlspecialchars(strip_tags($panelist_review_event_id,ENT_QUOTES));
				$insert['panelist_review_award_id'] = htmlspecialchars(strip_tags($panelist_review_award_id,ENT_QUOTES));
				$insert['panelist_review_panelist_id'] = htmlspecialchars(strip_tags($panelist_review_panelist_id,ENT_QUOTES));
				$insert['panelist_review_applicant_id'] =htmlspecialchars(strip_tags($panelist_review_applicant_id,ENT_QUOTES));
				$insert['panelist_review_created_date'] = date('Y-m-d H:i:s');
				$insert['panelist_review_status'] = 'Active';
				$this->db->insert('scopus_panelist_review',$insert);
			}
		return true;
	}
	
	public function get_score_panelist($award_id){
		$this->db->where('panelist_review_award_id',$award_id);
		$this->db->where('panelist_review_panelist_id',$this->session->userdata('admin_id'));
		$this->db->where('panelist_review_status','Active');
		$query=$this->db->get('scopus_panelist_review');
		$q = $query->result_array();
		return $q;
	}
	
	public function panelist_score_excel(){
		$this->db->where('panelist_review_status','Active');
		$query=$this->db->get('scopus_panelist_review');
		return $query->result();
	}
	
	public function fetch_eventname($i){
		$this->db->where('event_id',$i);
		$this->db->where('event_status','Active');
		$query1=$this->db->get('scopus_event');
		return $query1->result();
	}
	public function get_applicant($award_id){
		$this->db->where('admin_review_award_id',$award_id);
		$this->db->where('admin_review_status','Active');
		$query = $this->db->get('scopus_admin_review');
		$result = $query->result_array();
		if($result!=NULL) { 
			for($i= 0; $i < count($result); $i++){
				$this->db->where('user_application_applicant_id',$result[$i]['admin_review_applicant_id']);
				$this->db->where('user_application_award_id',$award_id);
				$query = $this->db->get('scopus_user_application');
				$row[$i] = $query->result_array();
			}
			return $row;
		}
	}
	
	public function get_adminScore($award_id){
		$this->db->where('admin_review_award_id',$award_id);
		$this->db->where('admin_review_status','Active');
		$query = $this->db->get('scopus_admin_review');
		$result = $query->result_array();
		return $result;
	}
}